package org.nf.ssm.web.controller.borrow;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.nf.ssm.common.ResultVO;
import org.nf.ssm.domain.Borrow;
import org.nf.ssm.service.BorrowService;
import org.nf.ssm.web.controller.BaseController;
import org.nf.ssm.web.vo.PageVO;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhu
 * @date 2024-10-17
 */
@RestController
@RequiredArgsConstructor
public class BorrowController extends BaseController {

    private final BorrowService borrowService;

    @GetMapping("/borrow/getAllBorrow")
    public PageVO getAllBorrow(@RequestParam("page") Integer pageNum,
                               @RequestParam("limit") Integer pageSize,
                               @RequestParam(value = "bookName",required = false) String bookName,
                               @RequestParam(value = "borrowId",required = false) String borrowId,
                               @RequestParam(value = "borrowType",required = false) String borrowType,
                               @RequestParam(value = "typeId",required = false) Integer typeId) {
        PageVO pageVO = new PageVO();
        PageInfo<Borrow> borrowPageInfo = borrowService.getAllBorrow(pageNum, pageSize, bookName, borrowId, borrowType, typeId);
        pageVO.setTotal(borrowPageInfo.getTotal());
        pageVO.setPageNum(borrowPageInfo.getPageNum());
        pageVO.setPageSize(borrowPageInfo.getPageSize());
        pageVO.setCode(200);
        pageVO.setData(borrowPageInfo);
        return pageVO;
    }

    @PostMapping("/borrow/addBorrow")
    public ResultVO addBorrow(@RequestBody Borrow borrow){
        borrowService.addBorrow(borrow);
        return success();
    }

    @PostMapping("/borrow/updateBorrow")
    public ResultVO updateBorrow(@RequestBody Borrow borrow){
        borrowService.updateBorrow(borrow);
        return success();
    }

    @PostMapping("/borrow/deleteBorrow")
    public ResultVO deleteBorrow(Integer index){
        borrowService.deleteBorrow(index);
        return success();
    }
}
